package com.tocea.formation.tpLibraryEjb.service;

import java.util.List;
import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.jws.WebService;
import com.tocea.formation.tpLibraryEjb.DTO.BookDTO;
import com.tocea.formation.tpLibraryEjb.eao.BookEAO;
import com.tocea.formation.tpLibraryEjb.exception.BookNotFoundException;
import com.tocea.formation.tpLibraryEjb.util.BookConvert;

@Stateless
@LocalBean
@WebService
public class BookBean implements IBookBean {

	@EJB
	private BookEAO eao;

	@Override
	public void addBook(BookDTO b) {
		eao.addBook(b.getName(), b.getGenre(), b.getAuthor().getFirstname(), b
				.getAuthor().getLastname(), b.getIsbn(), b.getStock());
	}

	@Override
	public void deleteBook(BookDTO b) {
		eao.deleteBook(b.getIsbn());
	}

	@Override
	public List<BookDTO> findBook(String s) {
		return BookConvert.convertFromListBookToListDTO(eao.findBookByName(s));

	}

	@Override
	public void updateBook(BookDTO b) {
		eao.updateBook(b.getIsbn(), b.getName(), b.getGenre(), b.getStock());
	}

	@Override
	public boolean exists(String nom) {
		return eao.exists(nom);
	}
	
	@Override
	public List<BookDTO> getAll(){
		return BookConvert.convertFromListBookToListDTO(eao.getAll());
	}
	
	@Override
	public void addExemplaires(String isbn, Integer ex){
		try {
			eao.addExemplaire(isbn, ex);
		} catch (BookNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@Override
	public void removeExemplaires(String isbn, Integer ex){
		try{
			eao.removeExemplaire(isbn, ex);
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}

}
